<template>
  <div>
    <Menu menuActive="/faq" />
    <layout isFlex>
      <template v-slot:main>
        <el-row>
          <el-col>
            <div class="top_filter_container height_light_bgcolor">
              <div style="text-align: right;">
                <el-button type="primary" :icon="Plus" @click="() => { router.push({ path: '/upload-question', }) }">{{$t('faqPage.publishBtn')}}
                </el-button>
              </div>
              <div class="filter_search_wrap">
                <span class="filter_name">搜索：</span>
                <div class="filter_search" style="width:500px">
                  <el-input placeholder="请输入问题名称" class="input-with-select" v-model="searchParams.title" clearable
                    @clear="handleClearSearch">
                    <template #append>
                      <el-button :icon="Search" @click="handleSerach" />
                    </template>
                  </el-input>
                </div>
              </div>
              <div class="first_fiter_wrap">
                <span class="filter_name" style="margin-top:0">频道：</span>
                <el-tag class="cursor_class tag_item" size="large"
                  :effect="currentIndexChannel === index && channelId === item.id ? 'dark' : 'light'"
                  :type="currentIndexChannel === index && channelId === item.id ? '' : 'info'"
                  v-for="(item, index) in channelData" :key="item.id" @click="handleClickChannelTag(index, item)">{{
                      item.name
                  }}</el-tag>
              </div>
              <div class="filter_tag_wrap">
                <span class="filter_name">标签：</span>
                <div class="filter_tag" style="width:500px">
                  <el-select v-model="searchParams.tags" @change="handleChangeTags" style="width:100%;" multiple
                    collapse-tags placeholder="请选择标签,最多可选择5个" :multiple-limit="5">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
                  </el-select>
                </div>
              </div>
            </div>
            <div class="bottom_content_container height_light_bgcolor">
              <div class="tab_wrapper">
                <el-tabs v-model="activeName" @tab-click="handleClick">
                  <el-tab-pane :label="item.label" :name="item.name" v-for="item in tabPanel" :key="item.id">
                    <div class="tab_pane_wrapper" :style="{ padding: listLoading ? '45px 0' : '' }">
                      <div v-loading="listLoading" element-loading-text="正在玩命加载中...">
                        <Question v-for="item in faqList" :key="item.id" :title="item.title" :desc="item.description"
                          :id="item.id" :tags="item.tags" :num="item.answerNum" :time="item.createTime"
                          :view-number="item.pageViewNum" :author="item.user?.nickName"/>
                      </div>
                      <el-empty description="暂无数据~" v-if="!listLoading && listTotal <= 0" />
                      <div class="pagination_container">
                        <el-pagination v-model:current-page="searchParams.pageNum"
                          v-model:page-size="searchParams.pageSize" background layout="total, prev, pager, next, jumper"
                          :total="listTotal" v-if="listTotal > 0" @current-change="handleCurrentChange" />
                      </div>
                    </div>
                  </el-tab-pane>
                </el-tabs>
              </div>
            </div>
          </el-col>
        </el-row>
      </template>
    </layout>
    <Footer />
  </div>
</template>

<script lang="ts" setup>
import Menu from '@/components/Menu/index.vue'
import Layout from '@/components/Layout/index.vue'
import Footer from '@/components/Footer/index.vue'
import Article from '@/components/Article/index.vue'
import Question from '@/components/Question/index.vue'
import router from '@/router'
import { onMounted, reactive, ref, watch } from 'vue'
import type { TabsPaneContext } from 'element-plus'
import { getFaqList } from '@/api/faq'
import {
  Document,
  Menu as IconMenu,
  Location,
  Setting,
  Search,
  Plus
} from '@element-plus/icons-vue'
import { getTag, getChannel } from '@/api/dictionary'
import i18n from '@/i18n/index'
type Params = {
  pageNum: number,
  pageSize: number,
  channelId: number | null,
  tags: string[] | null,
  title: string | null
}
const listLoading = ref<boolean>(false)
const activeName = ref<any>('first')
const tabPanel = ref([{
  id: 1,
  name: 'first',
  // label: i18n.global.t('indexTitle.articleTitle2')
  label: '最新'
}, {
  id: 2,
  name: 'second',
  // label: i18n.global.t('indexTitle.articleTitle3')
  label: '热门'
}, {
  id: 3,
  name: 'third',
  // label: i18n.global.t('indexTitle.articleTitle4')
  label: '待回答'
}])
const options = ref<any[]>([])
const channelData = ref<any[]>([])
const currentIndexChannel = ref<number | null>(0)
const channelId = ref<number | null>(null)
const searchParams = reactive<Params>({
  pageNum: 1,
  pageSize: 10,
  channelId: null,
  tags: null,
  title: ''
})
const faqList = ref<Array<any>>([])
const listTotal = ref<number>(0)
const handleChangeTags = () => {
  getFaqListData()
}
// 搜索
const handleSerach = () => {
  searchParams.pageNum = 1
  activeName.value = 'first'
  getFaqListData()
}
const handleClearSearch = () => {
  searchParams.pageNum = 1
  searchParams.title = ''
  activeName.value = 'first'
  getFaqListData()
}
const handleClick = (tab: TabsPaneContext, event: Event) => {
  console.log(tab, event)
  searchParams.pageNum = 1
  activeName.value = tab.props.name
  if (activeName.value === 'first') {
    getFaqListData()
  } else if (activeName.value === 'second') {
    getFaqListData({ isHot: 1 })
  } else if (activeName.value === 'third') {
    getFaqListData({ searchValue: 1 })
  }
}
// 获取标签
const getTagData = async () => {
  const { data }: any = await getTag()
  options.value = data.map((item: { name: any; id: any }) => ({ label: item.name, value: item.id }))
}
// 获取频道
const getChannelData = async () => {
  const { data }: any = await getChannel()
  channelData.value = data
  channelId.value = data[0].id
  searchParams.channelId = data[0].id
  getFaqListData()
}
const handleClickChannelTag = (index: number, item: any) => {
  currentIndexChannel.value = index
  channelId.value = item.id
  searchParams.channelId = item.id
  searchParams.pageNum = 1
  activeName.value = 'first'
  getFaqListData()
}
// 获取问答列表
const getFaqListData = async (cate?: any) => {
  console.log(searchParams.tags);
  let tagStr = ''
  if (searchParams.tags) {
    tagStr = searchParams.tags.toString()
  }
  // console.log(tagStr);
  try {
    listLoading.value = true
    const { rows, total }: any = await getFaqList({ ...cate, ...searchParams, tags: tagStr })
    faqList.value = rows
    listTotal.value = total
  } finally {
    listLoading.value = false
  }

}
const handleCurrentChange = (val: number) => {
  searchParams.pageNum = val
  getFaqListData()
}
onMounted(() => {
  getTagData()
  getChannelData()
  getFaqListData()
})

</script>

<style scoped lang="scss">
// @import '@/assets/style/reset.scss';
@import './index.scss';
</style>
